package com.zz.schoolwall.controller;

import com.zz.common.utils.*;
import com.zz.schoolwall.domain.SchoolWallDO;
import com.zz.schoolwall.domain.SchoolWallDTO;
import com.zz.schoolwall.domain.SchoolWallMediaDO;
import com.zz.schoolwall.service.SchoolWallService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/schoolwall")
@Controller
public class SchoolWallController {
	@Autowired
	SchoolWallService schoolWallService;

	@GetMapping()
	String main() {
		return "schoolwall/index/main";
	}

	@GetMapping("/add")
	@RequiresPermissions("schoolwall:wall:add")
	String add() {
		return "schoolwall/index/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("schoolwall:wall:edit")
	String edit(@PathVariable("id") Long id, Model model) {
		model.addAttribute("id", id);
		return "schoolwall/info/edit";
	}
	@GetMapping("/edit/info/{id}")
	@RequiresPermissions("schoolwall:wall:edit")
	String editInfo(@PathVariable("id") Long id, Model model) {
		SchoolWallDO schoolWallDO = schoolWallService.get(id);
		model.addAttribute("schoolWallDO", schoolWallDO);
		return "schoolwall/info/edit_info";
	}
	@GetMapping("/list/{id}")
	String list(@PathVariable("id") Long id, Model model) {
		SchoolWallDO schoolWallDO = schoolWallService.get(id);
		model.addAttribute("schoolWallDO", schoolWallDO);
		return "schoolwall/index/post";
	}

	/**
	 * 查询列表
	 * @param params
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		params.put("wallStatus",1);
		Query query = new Query(params);
		List<SchoolWallDO> reList = schoolWallService.list(query);
		int total = schoolWallService.count(query);
		PageUtils pageUtils = new PageUtils(reList, total);
		return pageUtils;
	}




	@RequiresPermissions({"schoolwall:wall:add","schoolwall:wall:edit"})
	@ResponseBody
	@PostMapping("/save")
	R save( @RequestBody SchoolWallDTO schoolWallDTO) {
		for(SchoolWallMediaDO t:schoolWallDTO.getSysFileIds()){
			System.out.println(t.getSysFileId());
		}
		int count=-1;
		if (schoolWallDTO.getWallId() ==null) {
			count = schoolWallService.save(schoolWallDTO);
		} else {
			count = schoolWallService.update(schoolWallDTO);
		}
		if (count>0) {
			return R.ok().put("wallId", schoolWallDTO.getWallId());
		}
		return R.error();
	}


	/**
	 * 删除
	 */
	@RequiresPermissions("schoolwall:wall:remove")
	@PostMapping("/remove")
	@ResponseBody
	public R remove(Long id) {
		if (schoolWallService.logicRemove(id) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("schoolwall:wall:batchRemove")
	@PostMapping("/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] wallIds) {
		schoolWallService.batchLogicRemove(wallIds);
		return R.ok();
	}

}
